package controllers.validation.jobs;

import java.util.Date;

import models.UserConfig;
import play.Logger;
import play.Play;
import play.jobs.Job;
import util.MessageTextController;
import ch.bfh.abch.rems.REMSMulticastController;
import ch.bfh.abch.rems.sender.tinkerforge.TFMulticastSender;
import ch.bfh.abcm.rems.gateway.GateWayMessageType;
import ch.bfh.abcm.rems.gateway.GatewayMessage;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorType;

public class AbstratctValidationJob extends Job {

    private static final long DEFAULTE_MAX_VALUE_USER = 1;
    private static final long DEFAULTE_MIN_VALUE_USER = 0;
    
    protected static String NODE_IP = Play.configuration.getProperty("own.node.ip", "unknown");
    protected org.apache.log4j.Logger validationJobLogger = org.apache.log4j.Logger.getLogger("validationJob");
    protected String logprefix = "";
    protected TFMulticastSender sender;
    protected MessageTextController mtc;
    protected TFSensorType sensorType;
    protected String measurementpoint;

    public AbstratctValidationJob(String measurementpoint) {
        this.sender = new REMSMulticastController().getMulticastSender();
        this.mtc = new MessageTextController();
        this.measurementpoint = measurementpoint;
    }

    protected boolean isComponentResponding(long timestamp, long maxTimeDiff, String component) {
        long now = new Date().getTime();
        long diff = now - timestamp;
        validationJobLogger.debug(logprefix + "Component " + component + " is Responding Diff:" + diff + " MaxDiff: "
                + maxTimeDiff + " >" + (diff < maxTimeDiff) + "<");
        return (diff < maxTimeDiff);
    }

    protected void createLogprefix(String prefix) {
        logprefix = "[" + prefix + "] ";
    }

    protected long getUserPropertyLong(String key, long defaultValue) {
        UserConfig userproperty = UserConfig.getProperty(key);
        if (userproperty != null) {
            try {
                long value = Long.valueOf(userproperty.getPropertyValue());
                        Logger.debug("get Property for Key: %s > Value: %s", key, value);
                return value;
            } catch (Exception e) {
                Logger.warn("Propterty casting failed, returning defaultValue: %s", defaultValue);
                return defaultValue;
            }
        } else {
            Logger.warn("Propterty is NULL, returning defaultValue: %s", defaultValue);
            return defaultValue;
        }
    }

    protected int getUserPropertyInt(String key, long defaultValue) {
        return (int) getUserPropertyLong(key, defaultValue);
    }

    protected void sendGatewayMessage(String text, GateWayMessageType type, TFSensorType sensorType) {
        GatewayMessage message = new GatewayMessage(type, text, sensorType);
        validationJobLogger.info("Send " + message.toString());
        sender.sendGateWayMessage(message);
    }
    
    protected boolean isInRange(int value , TFSensorType sensorType) {
        String keyusermax= sensorType + UserConfig.MAX_VALUE_USER_KEY_EXT;
        String keyusermin= sensorType + UserConfig.MIN_VALUE_USER_KEY_EXT;
        Logger.debug(keyusermin + " "+ keyusermax);
        int ucmin = getUserPropertyInt(keyusermin, DEFAULTE_MIN_VALUE_USER);
        int ucmax = getUserPropertyInt(keyusermax, DEFAULTE_MAX_VALUE_USER);
        return (value > ucmin && value < ucmax);
    }
}